package com.shuai.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RemoteViews;

import com.shuai.adapter.MemoryRemoteViewFactory;
import com.shuai.adapter.MemoryService;
import com.shuai.adapter.PlantListService;
import com.shuai.adapter.PlantRemoviewFactory;
import com.shuai.adapter.TaskListService;
import com.shuai.adapter.TaskRemoteViewFactory;
import com.shuai.sql.SQL;
import com.shuai.stickynote.EditActivity;
import com.shuai.stickynote.MemoryNote;
import com.shuai.stickynote.PlantNote;
import com.shuai.stickynote.R;
import com.shuai.stickynote.TaskNote;
import com.shuai.stickynote.TextNote;
import com.shuai.tool.ClassFactory;
import com.shuai.tool.MyTime;
import com.shuai.tool.SendTool;
import com.shuai.ui.MyRemoteViews;

import java.util.ArrayList;

import static android.content.Context.MODE_PRIVATE;

public class Widget_2x2 extends AppWidgetProvider {
    private String TAG = "fuck";
    private String type = "";
    private int id = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        String action = intent.getAction();
        if (TextUtils.equals(action,SendTool.WIDGET_ACTION_TEXT) ||
                TextUtils.equals(action,SendTool.WIDGET_ACTION_PLANT) ||
                TextUtils.equals(action,SendTool.WIDGET_ACTION_TASK) ||
                TextUtils.equals(action,SendTool.WIDGET_ACTION_MENORY)){
            RemoteViews views = null;
            type = intent.getStringExtra(SendTool.TYPE);
            String noteId = intent.getStringExtra(SendTool.NOTE_ID);
            int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
            if (id == 0) id = widgetId;
            switch (type){
                case SendTool.TYPE_TEXT:
                    views = setTypeText(context,widgetId,noteId);
                    openACT(context,views,widgetId,R.id.text_widget_click);
                    break;
                case SendTool.TYPE_PLANT:
                    views = setTypePlant(context,widgetId,noteId);
                    openACT(context,views,widgetId,R.id.widget_plant_click);
                    break;
                case SendTool.TYPE_TASK:
                    views = setTypeTask(context,widgetId,noteId);
                    openACT(context,views,widgetId,R.id.widget_task_click);
                    break;
                case SendTool.TYPE_MEMORY:
                    views = setTypeMemory(context,widgetId,noteId);
                    openACT(context,views,widgetId,R.id.widget_memory_click);
                    break;
                default:
                    views = new MyRemoteViews(context.getPackageName(),R.layout.default_widget);
                    openACT(context,views,widgetId,R.id.default_widget_click);
                    break;
            }
            AppWidgetManager.getInstance(context).updateAppWidget(widgetId,views);
        }
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        SharedPreferences sp = context.getSharedPreferences("info", MODE_PRIVATE);
        for (int appWidgetId : appWidgetIds) {
            type = sp.getString(appWidgetId+SendTool.TYPE,"");
            RemoteViews views = null;
            switch (type){
                case SendTool.TYPE_TEXT:
                    views = updateWidgetForText(context,appWidgetId);
                    openACT(context,views,appWidgetId,R.id.text_widget_click);
                    break;
                case SendTool.TYPE_PLANT:
                    views = updateWidgetForPlant(context,appWidgetId);
                    openACT(context,views,appWidgetId,R.id.widget_plant_click);
                    break;
                case SendTool.TYPE_TASK:
                    views = updateWidgetForTask(context,appWidgetId);
                    openACT(context,views,appWidgetId,R.id.widget_task_click);
                    break;
                case SendTool.TYPE_MEMORY:
                    views = updateWidgetForMemory(context,appWidgetId);
                    openACT(context,views,appWidgetId,R.id.widget_memory_click);
                    break;
                default:
                    views = new MyRemoteViews(context.getPackageName(),R.layout.default_widget);
                    openACT(context,views,appWidgetId,R.id.default_widget_click);
                    break;
            }
            appWidgetManager.updateAppWidget(appWidgetId,views);
        }
    }
//    upDate区
    private RemoteViews updateWidgetForText(Context context, int widgetId){
        AppWidgetManager awm = AppWidgetManager.getInstance(context);
        SharedPreferences sp = context.getSharedPreferences("info", MODE_PRIVATE);
        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widget_text);
        String noteId = sp.getString(widgetId+SendTool.NOTE_ID,"");
        if (!TextUtils.equals(noteId,"")){
            SQL sql = new SQL(context);
            TextNote text = sql.selectTextById(noteId);
            views.setTextViewText(R.id.text_widget_title,text.getTitle());
            views.setTextViewText(R.id.text_widget_content, text.getContent());
        }
        awm.updateAppWidget(widgetId,views);
        return views;
    }
    private RemoteViews updateWidgetForPlant(Context context, int widgetId){
        AppWidgetManager awm = AppWidgetManager.getInstance(context);
        ComponentName cn = new ComponentName(context,Widget_2x2.class);
        SharedPreferences sp = context.getSharedPreferences("info", MODE_PRIVATE);
        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widget_plant);
        SQL sql = new SQL(context);
        String noteId = sp.getString(widgetId+SendTool.NOTE_ID,"");
        ArrayList<PlantNote> plants = sql.selectPlantByDay(noteId);
        String day = MyTime.toDateString(noteId);
        views.setTextViewText(R.id.widget_plant_title,day);
        Intent intent = new Intent(context, PlantListService.class);
        intent.putExtra("day",noteId);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,widgetId);
        views.setRemoteAdapter(R.id.widget_plant_list_2,intent);
        PlantRemoviewFactory.plants.addAll(plants);
        awm.notifyAppWidgetViewDataChanged(awm.getAppWidgetIds(cn),R.id.widget_plant_list_2);
        return views;
    }
    private RemoteViews updateWidgetForMemory(Context context, int widgetId) {
        AppWidgetManager awm = AppWidgetManager.getInstance(context);
        ComponentName cn = new ComponentName(context,Widget_2x2.class);
        SharedPreferences sp = context.getSharedPreferences("info", MODE_PRIVATE);
        String group = sp.getString(widgetId+SendTool.NOTE_ID,"");
        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widget_memory);
        SQL sql = new SQL(context);
        ArrayList<MemoryNote> memorys = sql.selectMemoryByGroup(group);
        Intent intent = new Intent(context, MemoryService.class);
        intent.putExtra("group",group);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,widgetId);
        views.setRemoteAdapter(R.id.widget_memory_list,intent);
        MemoryRemoteViewFactory.memorys.addAll(memorys);
        awm.notifyAppWidgetViewDataChanged(awm.getAppWidgetIds(cn),R.id.widget_memory_list);
        return views;
    }
    private RemoteViews updateWidgetForTask(Context context, int widgetId) {
        ComponentName cn = new ComponentName(context,Widget_2x2.class);
        AppWidgetManager awm = AppWidgetManager.getInstance(context);
        SharedPreferences sp = context.getSharedPreferences("info", MODE_PRIVATE);
        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widget_task);
        String group = sp.getString(widgetId+SendTool.NOTE_ID,"");
        if (!TextUtils.equals(group,"")){
            SQL sql = new SQL(context);
            ArrayList<TaskNote> tasks = sql.selectTaskByGroup(group);
            if (tasks.get(0).getTitle().equals(""))
                views.setTextViewText(R.id.widget_task_title,"我的目标");
            else views.setTextViewText(R.id.widget_task_title,tasks.get(0).getTitle());
            Intent intent = new Intent(context, TaskListService.class);
            intent.putExtra("group",group);
            views.setRemoteAdapter(R.id.widget_task_list,intent);
            TaskRemoteViewFactory.tasks.addAll(tasks);
            awm.notifyAppWidgetViewDataChanged(awm.getAppWidgetIds(cn),R.id.widget_task_list);
        }
        return views;
    }
//    onReceive区
    private RemoteViews setTypePlant(Context context, int widgetId, String noteId) {
        AppWidgetManager awm = AppWidgetManager.getInstance(context);
        ComponentName cn = new ComponentName(context,Widget_2x2.class);
        SharedPreferences sp = context.getSharedPreferences("info", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widget_plant);
        SQL sql = new SQL(context);
        ArrayList<PlantNote> plants = sql.selectPlantByDay(noteId);
        String day = MyTime.toDateString(noteId);
        views.setTextViewText(R.id.widget_plant_title,day);
        Intent intent = new Intent(context, PlantListService.class);
        intent.putExtra("day",noteId);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,widgetId);
        views.setRemoteAdapter(R.id.widget_plant_list_2,intent);
        PlantRemoviewFactory.plants.clear();
        PlantRemoviewFactory.plants.addAll(plants);
        awm.notifyAppWidgetViewDataChanged(awm.getAppWidgetIds(cn),R.id.widget_plant_list_2);
        Log.d(TAG, "setTypePlant: "+(intent == null));
        editor.putString(widgetId+SendTool.NOTE_ID,noteId);
        editor.putString(widgetId+SendTool.TYPE,type);
        editor.commit();
        return views;
    }
    private RemoteViews setTypeText(Context context, int widgetId, String noteId) {
        ComponentName cn = new ComponentName(context,Widget_2x2.class);
        AppWidgetManager awm = AppWidgetManager.getInstance(context);
        SharedPreferences sp = context.getSharedPreferences("info",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        RemoteViews views = null;
        TextNote data = null;
        SQL sql = new SQL(context);
        views = SendTool.RemoteViewsFactory.getRemoteViews(context,type);
        data = sql.selectTextById(noteId);
        views.setTextViewText(R.id.text_widget_title,data.getTitle());
        views.setTextViewText(R.id.text_widget_content,data.getContent());
        editor.putString(widgetId+SendTool.NOTE_ID,data.getId());
        editor.putString(widgetId+SendTool.TYPE,type);
        editor.commit();
        awm.updateAppWidget(widgetId,views);
        return views;
    }
    private RemoteViews setTypeMemory(Context context, int widgetId, String noteId) {
        AppWidgetManager awm = AppWidgetManager.getInstance(context);
        ComponentName cn = new ComponentName(context,Widget_2x2.class);
        SharedPreferences sp = context.getSharedPreferences("info", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widget_memory);
        SQL sql = new SQL(context);
        ArrayList<MemoryNote> memorys = sql.selectMemoryByGroup(noteId);
        Intent intent = new Intent(context, MemoryService.class);
        intent.putExtra("group",noteId);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,widgetId);
        views.setRemoteAdapter(R.id.widget_memory_list,intent);
        MemoryRemoteViewFactory.memorys.addAll(memorys);
        awm.notifyAppWidgetViewDataChanged(awm.getAppWidgetIds(cn),R.id.widget_memory_list);
        editor.putString(widgetId+SendTool.NOTE_ID,noteId);
        editor.putString(widgetId+SendTool.TYPE,type);
        editor.commit();
        return views;
    }
    private RemoteViews setTypeTask(Context context, int widgetId, String noteId) {
        ComponentName cn = new ComponentName(context,Widget_2x2.class);
        AppWidgetManager awm = AppWidgetManager.getInstance(context);
        SharedPreferences sp = context.getSharedPreferences("info", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widget_task);
        if (!TextUtils.equals(noteId,"")){
            SQL sql = new SQL(context);
            ArrayList<TaskNote> tasks = sql.selectTaskByGroup(noteId);
            if (tasks.get(0).getTitle().equals(""))
                views.setTextViewText(R.id.widget_task_title,"我的目标");
            else views.setTextViewText(R.id.widget_task_title,tasks.get(0).getTitle());
            Intent intent = new Intent(context, TaskListService.class);
            intent.putExtra("group",noteId);
            views.setRemoteAdapter(R.id.widget_task_list,intent);
            TaskRemoteViewFactory.tasks.clear();
            TaskRemoteViewFactory.tasks.addAll(tasks);
            awm.notifyAppWidgetViewDataChanged(awm.getAppWidgetIds(cn),R.id.widget_task_list);
        }
        editor.putString(widgetId+SendTool.NOTE_ID,noteId);
        editor.putString(widgetId+SendTool.TYPE,type);
        editor.commit();
        return views;
    }

    private void openACT(Context context, RemoteViews views, int widgetId, int clickView){
        Intent intent = new Intent(context, EditActivity.class);
        intent.setAction(SendTool.WIDGET_ACTION_TEXT);
        intent.putExtra("id",widgetId);
        intent.putExtra(ClassFactory.SIZE,ClassFactory.SIZE_2X2);
        PendingIntent pi = PendingIntent.getActivity(context,widgetId,intent,PendingIntent.FLAG_CANCEL_CURRENT);
        views.setOnClickPendingIntent(clickView,pi);
    }
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
        SharedPreferences sp = context.getSharedPreferences("info", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        for (int id : appWidgetIds) {
            editor.remove(id+SendTool.NOTE_ID);
            editor.remove(id+SendTool.TYPE);
            editor.commit();
        }
    }
}

